package com.dzt.cloudreader.adapter;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dzt.cloudreader.R;
import com.dzt.cloudreader.activity.OneMovieDetailActivity;
import com.dzt.cloudreader.adapter.recycler.RecyclerAdapter;
import com.dzt.cloudreader.adapter.recycler.RecyclerHolder;
import com.dzt.cloudreader.interfaces.PerfectClickListener;
import com.dzt.cloudreader.model.HotMovieBean;

import java.util.List;

/**
 * Created by M02323 on 2018/1/15.
 */

public class DouBanTopAdapter extends RecyclerAdapter<HotMovieBean.SubjectsBean> {

    private Activity activity;

    public DouBanTopAdapter(Activity activity, List<HotMovieBean.SubjectsBean> datas, int layoutId) {
        super(activity, datas, layoutId);
        this.activity = activity;
    }

    @Override
    public void convert(final RecyclerHolder holder, final HotMovieBean.SubjectsBean data, final int position) {
        holder.setImageGlide(R.id.iv_top_photo, data.getImages().getLarge());
        final ImageView ivTopPhoto = holder.findView(R.id.iv_top_photo);
        holder.setText(R.id.tv_name, data.getTitle());
        String rate = activity.getString(R.string.string_rating) + data.getRating().getAverage();
        holder.setText(R.id.tv_rate, rate);
        LinearLayout llItemTop = holder.findView(R.id.ll_item_top);
        llItemTop.setOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                OneMovieDetailActivity.start(activity, data, ivTopPhoto);
            }
        });
        llItemTop.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                View view = View.inflate(v.getContext(), R.layout.title_douban_top, null);
                TextView titleTop = view.findViewById(R.id.title_top);
                titleTop.setText("Top" + (position + 1) + ": " + data.getTitle());
                builder.setCustomTitle(view);
                final ImageView ivTopPhoto = holder.findView(R.id.iv_top_photo);
                builder.setPositiveButton("查看详情", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        OneMovieDetailActivity.start(activity, data, ivTopPhoto);
                    }
                });
                builder.show();
                return false;
            }
        });
    }
}
